package cn.itsource.order.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.order.domain.OrderPetAcquisition;
import cn.itsource.order.dto.OrderDto;
import cn.itsource.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.order.query.OrderPetAcquisitionQuery;
import cn.itsource.order.service.IOrderPetAcquisitionService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        // 判断登录员工是那种角色
        //1.employee中shop_id为null，平台管理员
        Long shopId = employee.getShopId();
        //2.店铺人员
        if (shopId!=null){
            Shop shop = shopMapper.findById(shopId);
            if(!shop.getAdminId().equals(employee.getId())){//店员 //注意：Long包装类不能用==判断，long才行
                //3.否则就为店员
                query.setEmpId(employee.getId());
            }
            //4.employee中shop_id不为null，通过shop_id查询店铺得到admin_id如果为当前登录管理员的id，就为店铺管理员
            query.setShopId(shopId);
        }
        //5.平台管理员
        return super.queryPage(query);
    }

    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {
        //1.改订单状态 0待处理 - 1已处理
        //2.修改订单的支付方式和金
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(orderDto.getId());
        order.setState(1);
        order.setPaytype(orderDto.getPaytype());
        order.setPrice(orderDto.getMoney());
        orderPetAcquisitionMapper.update(order);


        //3.改寻主消息状态 1待处理 -2已完成
        SearchMasterMsg msg = searchMasterMsgMapper.findById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        //4.修改pet的成本价
        Pet pet = petMapper.findById(order.getPetId());
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);
    }

    @Override
    @Transactional
    public void cancel(Long id) { //订单id
        OrderPetAcquisition order = orderPetAcquisitionMapper.findById(id);
        //1.shopId置空
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //2.删除宠物信息 - 订单取消了，宠物就没有接回来。之前已经将宠物录入系统了
        petMapper.delete(order.getPetId());

        //3.改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }
}
